package top.drinyeit.weilong.presenter;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;

import java.util.Map;

import top.drinyeit.weilong.R;
import top.drinyeit.weilong.activity.StoreInfoActivity;
import top.drinyeit.weilong.model.StoreInfo;
import top.drinyeit.weilong.retrofit.HttpResponse;
import top.drinyeit.weilong.retrofit.RetrofitMethod;
import top.drinyeit.weilong.retrofit.subscriber.CommonSubscriber;
import top.drinyeit.weilong.retrofit.subscriber.SubscriberListener;

import static top.drinyeit.weilong.app.Constant.COMMON_SUCCESS;

/**
 * Created by wu on 2018/1/10.
 */

public class StoreInfoPresenter {
    private StoreInfoActivity activity;
    public StoreInfoPresenter(StoreInfoActivity activity){
        this.activity = activity;
    }

    //店铺信息
    public void getStoreInfo(Map<String,Object> map) {
        activity.showLoading();
        CommonSubscriber<HttpResponse<StoreInfo>> subscriber = new CommonSubscriber<>(new SubscriberListener<HttpResponse<StoreInfo>>() {
            @Override
            public void onError(String e, int code) {
                activity.dismissLoading();
                activity.showToastMsg("错误，请重新操作！");
            }

            @Override
            public void onNext(HttpResponse<StoreInfo> o) {
                activity.dismissLoading();
                if (o.getCode() == COMMON_SUCCESS) {
                    Glide.with(activity).load(o.getData().getHead_photo_url())
                            .dontTransform()
                            .placeholder(R.drawable.ic_def)
                            .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .into(activity.head_photo);
                    activity.store_name.setText(o.getData().getStore_name());
                    activity.store_number.setText(o.getData().getStore_number());
                    activity.score.setText(o.getData().getScore());
                    activity.store_open_time.setText(o.getData().getStore_open_time());
                    activity.description_score.setText(o.getData().getDescription_score());
                    activity.service_score.setText(o.getData().getService_score());
                    activity.logistics_score.setText(o.getData().getLogistics_score());
                    Glide.with(activity).load(o.getData().getSanitary_certificate_url()).placeholder(R.drawable.ic_def).dontTransform().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .into(activity.sanitary_certificate);
                    Glide.with(activity).load(o.getData().getBusiness_license_url()).placeholder(R.drawable.ic_def).dontTransform().diskCacheStrategy(DiskCacheStrategy.SOURCE)
                            .into(activity.business_license);
                }else {
                    activity.showToastMsg(o.getMessage());
                }
            }
        });
        RetrofitMethod.getInstance().getStoreInfo(subscriber, map);
    }
}
